﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net_SCADA
{
    public delegate void SetLabelDelegate(string v1 , string v2);

    public partial class fdCtrZone : UserControl
    {
        private string _unit ="℃";
        private string _text="";
        private int _showType=0;
        private int _colorType=0;
        private int _dataType=0;
        private int _v1id=0;
        private int _v2id=0;
        private int _zoneID=0;
        private Font _font=new Font("Arial", 9f);


        private StringBuilder _v1=new StringBuilder("9");
        private StringBuilder _v2=new StringBuilder("10");

        private bool _isZoneAlarm=false;
        public string Unit { get { return _unit; } set { _unit = value; showTitle(); } }
        public override string Text { get { return _text; } set { _text = value; showTitle(); } }//设置标题
        public int V1ID { get { return _v1id; } set {  _v1id = value; } } 
        public int V2ID { get { return _v2id; } set {  _v2id = value; } } 
        public int ShowType { get { return _showType; } set { _showType = value; reSetInterface(); } }
        public int ColorType { get { return _colorType; } set { _colorType = value; reSetInterface(); } }
        public int DataType { get { return _dataType; } set { _dataType = value; } }
        public int ZoneID { get { return _zoneID; } set { _zoneID = value; } }
        public List<int> AlarmID { get; set; } = new List<int>();
        public override Font Font { get => _font; set { _font = value; lbl_Name.Font = value; } }
        private Color[] clr=new Color[5];
        public bool IsZoneAlarm
        {
            get { return _isZoneAlarm; }
            set
            {
                if (_isZoneAlarm != value)
                {
                    if (value)
                    {
                         lbl_Value1.BackColor = lbl_Value2.BackColor = Color.DarkRed;
                         lbl_Value1.ForeColor = lbl_Value2.ForeColor = Color.Yellow;
                    }
                    else
                    {
                        lbl_Value2.BackColor = lbl_Value1.BackColor = clr[2];
                        lbl_Value1.ForeColor = clr[3];
                        lbl_Value2.ForeColor = clr[4];
                    }
                    _isZoneAlarm = value;
                }
            }
        }

        public fdCtrZone() { InitializeComponent(); }
        public fdCtrZone(string title,string unit)
        {
            InitializeComponent();
            lbl_Name.Text = title + " " + unit;
        }

        private void fdCtrZone_Resize(object sender, EventArgs e)
        {
            reSetInterface();
        }
        private void showTitle()
        {
            lbl_Name.Text = _text + " " + _unit;
        }
        private void reSetInterface()
        {
            int w=0;int h=0;
            switch (_showType)
            {
                case 1:
                    w = this.Width/2;
                    h = this.Height/2;
                    lbl_Name.Location = new Point(0 , 0);
                    lbl_Name.Size = new Size(this.Width , h);
                    lbl_Value1.Location = new Point(0 , lbl_Name.Height );
                    lbl_Value2.Location = new Point(w , lbl_Name.Height );
                    lbl_Value1.Size = new Size(w , this.Height - lbl_Name.Height - 1);
                    lbl_Value2.Size = new Size(this.Width - w , this.Height - lbl_Name.Height - 1);
                    lbl_Value1.Font  = Getfont(this.Height - lbl_Name.Height - 1, "Arial" , FontStyle.Bold);
                    lbl_Value2.Font = Getfont(this.Height - lbl_Name.Height - 5, "Arial", FontStyle.Bold);
                    lbl_Value1.TextAlign = ContentAlignment.BottomLeft;
                    lbl_Value2.TextAlign = ContentAlignment.BottomRight;

                    break;
                case 2:
                    break;
                case 0:
                default:
                    w = this.Width;
                    h = this.Height/10;
                    lbl_Name.Location = new Point(0 , 0);
                    lbl_Name.Size = new Size(w , 3 * h);
                    lbl_Value1.Location = new Point(0 , 3 * h);
                    lbl_Value1.Size = new Size(w , 5 * h);
                    lbl_Value1.Font = Getfont(5 * h, "Arial" , FontStyle.Bold);
                    lbl_Value1.TextAlign = ContentAlignment.MiddleCenter;
                    lbl_Value2.Location = new Point(0 , 7 * h);
                    lbl_Value2.Size = new Size(w , this.Height - 7 * h - 1);
                    lbl_Value2.Font = Getfont(this.Height - 7 * h - 1, "Arial" , FontStyle.Bold);
                    lbl_Value2.TextAlign = ContentAlignment.MiddleRight;
                    break;
            }

            switch (_colorType)
            {
                case 1:
                    lbl_Value1.ForeColor = lbl_Value2.ForeColor = _isZoneAlarm ? Color.Yellow : Color.DimGray;
                    lbl_Value1.BackColor = lbl_Value2.BackColor = _isZoneAlarm ? Color.DarkRed : clr[2];
                    break;
                case 0:
                default:
                    lbl_Value1.ForeColor = _isZoneAlarm ? Color.Yellow : clr[3];
                    lbl_Value2.ForeColor = _isZoneAlarm ? Color.Yellow : clr[4];
                    lbl_Value1.BackColor = lbl_Value2.BackColor = _isZoneAlarm ? Color.DarkRed : clr[2];
                    break;
            }
            SetValues(_v1.ToString() , _v2.ToString());
        }

        public void SetValues(string v1 , string v2)
        {
            if (this.InvokeRequired)
            {
                SetLabelDelegate d = new SetLabelDelegate(SetValues);
                this.Invoke(d , new object[] { v1 , v2 });
            }
            else
            {
                _v1.Clear().Append(v1);
                _v2.Clear().Append(v2);
                switch (_dataType)
                {
                    case 1:
                        lbl_Value1.Text = "-" + _v1;// string.Format("{0:#0.##}", Convert.ToDouble(_v1.ToString()));
                        lbl_Value2.Text = "+" + _v2;
                        break;
                    case 0:
                    default:
                        lbl_Value1.Text =v1;// string.Format("{0:#0.##}", Convert.ToDouble(_v1.ToString()));
                        lbl_Value2.Text =v2;
                        break;
                }
            }
        }
        public void SetColor(Color[] color)
        {
            if (color.Length < 5) return;
            clr[0] = lbl_Name.BackColor = color[0];
            clr[1] = lbl_Name.ForeColor = color[1];
            clr[2] = lbl_Value2.BackColor = lbl_Value1.BackColor = color[2];
            clr[3] = lbl_Value1.ForeColor = color[3];
            clr[4] = lbl_Value2.ForeColor = color[4];
        }

        private void fdCtrZone_Load(object sender, EventArgs e)
        {
            lbl_Name.Font = Font;
            clr[0] = lbl_Name.BackColor;
            clr[1] = lbl_Name.ForeColor;
            clr[2] = Color.WhiteSmoke;
            clr[3] = Color.DarkGreen;
            clr[4] = Color.Tomato;

            reSetInterface();
        }


        private static Font Getfont(int height , string fonttype , FontStyle fontStyle)
        {
            Label alabel = new Label();
            SizeF sizeF;
            string str = "1234";
            float fontsize = 1f;
            Font font;
            Graphics g = alabel.CreateGraphics();
            g.PageUnit = GraphicsUnit.Pixel;
            while (true)
            {
                font = new Font(fonttype , fontsize , fontStyle);// new Font("Arial", fontsize, FontStyle.Bold);
                sizeF = g.MeasureString(str , font);
                if (sizeF.Height >= height)
                {
                    return font;
                }
                fontsize += 0.5f;
            }
        }
    }
}
